package hoan.map;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class HoanMapDemoActivity extends MapActivity {

	private MapView mapView;
	private LocationManager locationManager;
	private LocationListener locationListener;
	private GeoPoint geoPoint;
	private List<Overlay> listOfOverlays;
	private MapOverlay mapOverlay;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		mapView.displayZoomControls(true);

		locationManager = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);
		locationListener = new MyLocationListener();
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
				0, locationListener);
		geoPoint = new GeoPoint((int) (10.7771246 * 1E6),
				(int) (106.701225 * 1E6));
		mapView.getController().animateTo(geoPoint);
		mapView.getController().setZoom(16);

		listOfOverlays = mapView.getOverlays();
		listOfOverlays.clear();
		mapOverlay = new MapOverlay();
		listOfOverlays.add(mapOverlay);
		mapView.invalidate();

	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	public void myChooseMapKindHandler(View target) {
		switch (target.getId()) {
		case R.id.satellite:
			mapView.setSatellite(true);
			break;
		case R.id.normal:
			mapView.setSatellite(false);
			mapView.setTraffic(false);
			break;
		default:
			break;
		}
		Toast.makeText(getApplicationContext(),
				"Bạn vừa thay đổi chế độ hiển thị bản đồ", Toast.LENGTH_SHORT)
				.show();
		mapView.postInvalidateDelayed(2000);
	}

	public void myFindMeHandler(View target) {
		switch (target.getId()) {
		case R.id.find_me:
			myFindMeLocationAndSetMapCenter();
			break;
		default:
			break;
		}
	}

	public void myFindMeLocationAndSetMapCenter() {
		if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
			Location location = locationManager
					.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
			if (location != null) {
				double latitude = location.getLatitude();
				double longitude = location.getLongitude();

				geoPoint = new GeoPoint((int) (latitude * 1E6),
						(int) (longitude * 1E6));
				mapView.getController().animateTo(geoPoint);
			}
		}
	}

	public class MyLocationListener implements LocationListener {

		@Override
		public void onLocationChanged(Location location) {
			if (location != null) {
				GeoPoint point = new GeoPoint(
						(int) (location.getLatitude() * 1E6),
						(int) (location.getLongitude() * 1E6));

				Toast.makeText(
						getBaseContext(),
						"Latitude: " + location.getLatitude() + " Longitude: "
								+ location.getLongitude(), Toast.LENGTH_SHORT)
						.show();

				mapView.getController().animateTo(point);
				mapView.getController().setZoom(16);
				mapView.invalidate();
			}
		}

		@Override
		public void onProviderDisabled(String provider) {
			Toast.makeText(getBaseContext(), "Bạn vừa tắt GPS",
					Toast.LENGTH_LONG);

		}

		@Override
		public void onProviderEnabled(String provider) {
			Toast.makeText(getBaseContext(), "Bạn vừa bật GPS",
					Toast.LENGTH_LONG);
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub

		}

	}

	class MapOverlay extends com.google.android.maps.Overlay {
		private List<MyMarker> listOfMarkers = new ArrayList<MyMarker>();
		private boolean mMoved = false;

		@Override
		public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
				long when) {
			super.draw(canvas, mapView, shadow);

			for (MyMarker mkr : listOfMarkers) {

				Point screenPts = new Point();
				mapView.getProjection().toPixels(mkr.getmGeoPoint(), screenPts);
				canvas.drawBitmap(mkr.getmBitmap(), screenPts.x,
						screenPts.y - 50, null);
			}
			return true;
		}

		@Override
		public boolean onTouchEvent(MotionEvent e, MapView mapView) {

			if (e.getAction() == MotionEvent.ACTION_UP) {
				if (mMoved == true) {
					mMoved = false;
					return false;
				}
				GeoPoint p = mapView.getProjection().fromPixels((int) e.getX(),
						(int) e.getY());

				String address = convertPointToLocation(p);

				Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT)
						.show();

				Bitmap bmp = BitmapFactory.decodeResource(getResources(),
						R.drawable.marker);
				MyMarker marker = new MyMarker();
				marker.setmBitmap(bmp);
				marker.setmGeoPoint(p);
				mapOverlay.listOfMarkers.add(marker);
				mapView.invalidate();
			}

			if (e.getAction() == MotionEvent.ACTION_MOVE) {
				mMoved = true;
			}
			return false;
		}

		public String convertPointToLocation(GeoPoint point) {
			String address = "";
			Geocoder geoCoder = new Geocoder(getBaseContext(),
					Locale.getDefault());
			try {
				List<Address> addresses = geoCoder.getFromLocation(
						point.getLatitudeE6() / 1E6,
						point.getLongitudeE6() / 1E6, 1);

				if (addresses.size() > 0) {
					for (int index = 0; index < addresses.get(0)
							.getMaxAddressLineIndex(); index++)
						address += addresses.get(0).getAddressLine(index) + " ";
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			return address;
		}

	}
}